package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.storage.TXSharedPreferencesUtils;
import com.tencent.cloud.uikit.ui.widget.IovToolbar;
import com.tencent.cloud.uikit.utils.DialogUtils;
import com.tencent.cloud.uikit.utils.SystemUiUtils;

import java.util.ArrayList;
import java.util.List;

import cn.sirun.com.fca.account.constant.CarConst;
import cn.sirun.com.fca.account.model.profile.Reseller;
import cn.sirun.com.fca.personal.center.R;
import cn.sirun.com.fca.personal.center.model.bean.CityInfo;
import cn.sirun.com.fca.personal.center.model.request.DistributorRequest;
import cn.sirun.com.fca.personal.center.model.request.EditResellerRequest;
import cn.sirun.com.fca.personal.center.model.response.CityResponse;
import cn.sirun.com.fca.personal.center.model.response.DistributorResponse;
import cn.sirun.com.fca.personal.center.model.service.PersonalInfoManager;
import cn.sirun.com.fca.personal.center.presenter.ISettingDistributorPresenter;
import cn.sirun.com.fca.personal.center.presenter.impl.SettingDistributorPresenter;
import cn.sirun.com.fca.personal.center.view.ISettingDistributorView;

public class SettingDistributorFlowView extends BaseLoadingFlowView<ISettingDistributorPresenter>
        implements ISettingDistributorView {

    private LinearLayout mAreaLL, mNameLL;
    private TextView mAreaTV, mNameTV;
    private OptionsPickerView mNameOPV;
    private OptionsPickerView mCityOPV;

    private List<CityResponse> mProvinceList;
    private ArrayList<List<CityInfo>> mCityList;
    private String mProvinceCode, mCityCode;
    private List<DistributorResponse> mDistributorList;

    private String mProvinceString, mCityString;
    private String mDistributorString;
    private Reseller mReseller;
    private String mResellerCode;

    public SettingDistributorFlowView(Activity activity) {
        super(activity);
    }

    public SettingDistributorFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    @Override
    public void onStart() {
        super.onStart();
        showLoadingView();
        getPresenter().getAllCity();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();

        final IovToolbar toolbar = SystemUiUtils.showActionBar(
                activity, ResourcesUtils.getString(R.string.setting_distributor), ResourcesUtils.getDrawable(R.color.white));
        toolbar.getmBackImageView().setVisibility(View.GONE);
        toolbar.getmBackTextView().setTextColor(ResourcesUtils.getColor(R.color.grey_A9));
        toolbar.setHomeAsUpText(R.string.cancel);
        toolbar.getmMenuTextView().setTextColor(ResourcesUtils.getColor(R.color.blue_5C));
        toolbar.setMenu(R.string.title_save, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleSaveClick();
            }
        });
    }

    @Override
    public void onGetAllCityResult(BaseResponse<List<CityResponse>> cityResponse) {
        hideLoadingView();
        int code = cityResponse.getCode();
        if (code == StatusCode.SUCCESS) {
            handleCityResponse(cityResponse);
            return;
        }
        DialogUtils.showToast(getActivity(), cityResponse.getMsg());
    }

    @Override
    public void onGetAllDistributorResult(BaseResponse<List<DistributorResponse>> distributorResponse) {
        hideLoadingView();
        int code = distributorResponse.getCode();
        if (code == StatusCode.SUCCESS) {
            handleDistributorResponse(distributorResponse);
            return;
        }
        DialogUtils.showToast(getActivity(), distributorResponse.getMsg());

    }

    @Override
    public void onEditDistributorResult(BaseResponse<Void> distributorResponse) {
        hideLoadingView();
        int code = distributorResponse.getCode();
        if (code == StatusCode.SUCCESS) {
            PersonalInfoManager.getInstance().updateReseller(mReseller);
            getActivity().finish();
            return;
        }
        DialogUtils.showToast(getActivity(), distributorResponse.getMsg());
    }

    private void handleDistributorResponse(BaseResponse<List<DistributorResponse>> distributorResponse) {
        mDistributorList = distributorResponse.getData();
        if (mDistributorList != null && mDistributorList.size() > 0) {
            initDistributorNamePicker();
        } else {
            DialogUtils.showToast(getActivity(), "所在城市暂无经销商");
        }
    }

    private void handleCityResponse(BaseResponse<List<CityResponse>> cityResponse) {
        mProvinceList = cityResponse.getData();
        if (mProvinceList != null && mProvinceList.size() > 0) {
            mCityList = new ArrayList<>();
            for (int i = 0; i < mProvinceList.size(); i++) {
                mCityList.add(mProvinceList.get(i).getCities());
            }
            initCityPicker();
        }
    }

    @Override
    protected ISettingDistributorPresenter createPresenter() {
        return new SettingDistributorPresenter();
    }

    private void initView() {
        setContentView(R.layout.setting_distributor_activity);
        final Activity activity = getActivity();
        mAreaLL = activity.findViewById(R.id.ll_area);
        mNameLL = activity.findViewById(R.id.ll_name);
        mAreaTV = activity.findViewById(R.id.tv_area_value);
        mNameTV = activity.findViewById(R.id.tv_name_value);

        registerListener();
    }

    private void handleSaveClick() {
        if (TextUtils.isEmpty(mProvinceString) || TextUtils.isEmpty(mCityString)) {
            DialogUtils.showToast(getActivity(), "请选择所在城市");
            return;
        }

        if (TextUtils.isEmpty(mDistributorString)) {
            DialogUtils.showToast(getActivity(), "请选择经销商");
            return;
        }

        showLoadingView();
        mReseller = new Reseller(mProvinceString, mCityString, mDistributorString, mResellerCode);
        String din = TXSharedPreferencesUtils.getValue(CarConst.SpKey.CAR_ACTIVATE_INFO);
        EditResellerRequest request = new EditResellerRequest(din, mReseller);
        getPresenter().editReseller(request);
    }

    private void registerListener() {
        mAreaLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCityOPV.show();
            }
        });

        mNameLL.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                handleDistributorLayoutClick();
            }
        });
    }

    private void handleDistributorLayoutClick() {
        if (TextUtils.isEmpty(mProvinceCode) || TextUtils.isEmpty(mCityCode)) {
            DialogUtils.showToast(getActivity(), "请先选择城市");
        } else {
            showLoadingView();
            getPresenter().getCityDistributor(new DistributorRequest(mProvinceCode, mCityCode));
        }
    }

    private void initCityPicker() {
        mCityOPV = new OptionsPickerBuilder(getActivity(), new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                mProvinceString = mProvinceList.get(options1).getPickerViewText();
                mCityString = mCityList.get(options1).get(option2).getPickerViewText();
                String areaString = mProvinceString + "  " + mCityString;
                mAreaTV.setText(areaString);
                mProvinceCode = mProvinceList.get(options1).getProvinceCode();
                mCityCode = mCityList.get(options1).get(option2).getCityCode();
            }
        })
                .setLayoutRes(R.layout.distributor_city_layout, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvCancle = (TextView) v.findViewById(R.id.tv_cancle);
                        final TextView tvTitle = (TextView) v.findViewById(R.id.tv_title);
                        TextView tvComplete = (TextView) v.findViewById(R.id.tv_complete);
                        tvComplete.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mCityOPV.returnData();
                                mCityOPV.dismiss();
                            }
                        });

                        tvCancle.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mCityOPV.dismiss();
                            }
                        });
                    }
                })
                .isDialog(false)
                .setCyclic(true, true, true)
                .setDividerColor(R.color.grey_DD)
                .setLineSpacingMultiplier(2f)
                .build();

        mCityOPV.setPicker(mProvinceList, mCityList);
    }

    private void initDistributorNamePicker() {
        mNameOPV = new OptionsPickerBuilder(getActivity(), new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                mDistributorString = mDistributorList.get(options1).getPickerViewText();
                mResellerCode = mDistributorList.get(options1).getDealerCode();
                mNameTV.setText(mDistributorString);
            }
        })
                .setLayoutRes(R.layout.distributor_city_layout, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvCancle = (TextView) v.findViewById(R.id.tv_cancle);
                        final TextView tvTitle = (TextView) v.findViewById(R.id.tv_title);
                        TextView tvComplete = (TextView) v.findViewById(R.id.tv_complete);
                        tvTitle.setText("选择经销商");
                        tvComplete.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mNameOPV.returnData();
                                mNameOPV.dismiss();
                            }
                        });

                        tvCancle.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mNameOPV.dismiss();
                            }
                        });
                    }
                })
                .isDialog(false)
                .setCyclic(true, true, true)
                .setDividerColor(R.color.grey_DD)
                .setLineSpacingMultiplier(2f)
                .build();

        mNameOPV.setPicker(mDistributorList);
        mNameOPV.show();
    }

}
